<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class PullAllUserFromOldLwa extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'pull:lwa-users';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '迁移原有 LWA 用户';

    protected $lwaDbClient;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->lwaDbClient = DB::connection('mysql-old-lwa');
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->lwaDbClient->table('users')->get()->each(function ($user) {
            $userExist = User::query()->where('phone', $user->phone)->exists();
            if ($userExist) {
                return;
            }
            User::query()->create([
                'phone' => $user->phone,
                'user_name' => $user->user_name,
                'password' => $user->password,
                'disable' => $user->disable,
                'role' => $user->role,
                'coupon' => 0,
                'email' => '',
                'headpic' => $user->headpic,
            ]);
        });
        $this->line('用户迁移完成');
        return 0;
    }
}
